package com.wenwei.sharebox.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wenwei.sharebox.R;

/**
 * Created by Administrator on 2017/07/12 0012.
 *
 * 认证
 */
public class AuthenticationPopup extends PopupWindow{
    public CallBack mCallBack;

    public interface CallBack{
        void onRightClick();
        void onLeftClick();
    }
    Activity mActivity;
    PopupWindow popupWindow;

    TextView okTv;
    TextView cancelTv;
    public AuthenticationPopup(Activity activity, View parentView, CallBack callBack) {
        this.mCallBack = callBack;
        mActivity = activity;
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.custom_authentication_popup, null);

        okTv = (TextView) contentView.findViewById(R.id.authentication_ok);
        okTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                mCallBack.onLeftClick();
            }
        });
        cancelTv = (TextView) contentView.findViewById(R.id.authentication_cancel);
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                mCallBack.onRightClick();
            }
        });

        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        ColorDrawable cd = new ColorDrawable(0x000000);
        popupWindow.setBackgroundDrawable(cd);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.4f;
        activity.getWindow().setAttributes(lp);
        popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);
            }
        });
    }

    /**
     * 改变背景颜色
     */
    private void darkenBackground(Float bgcolor) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mActivity.getWindow().setAttributes(lp);
    }
}
